﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Microsoft.AspNet.SignalR;
using System.Collections.Concurrent;

public class ChatHub : Hub
{
    static ConcurrentDictionary<string, string> ClientDirectory = new ConcurrentDictionary<string, string>();
    static BusinessLogicReference.BusinessLogicClient BL = new BusinessLogicReference.BusinessLogicClient();
    
    public void Send(string name, string message)
        //Sends user commands to the Server
    {
        BL.SendCommand(name, message);
    }
    public void Broadcast(string name, string message, string target)
        //Server uses to send broadcast messages to all users
    {
        Clients.All.broadcastMessage(name, message, target);
    }
    public void SendTo(string userID, string message, string target)
        //Server uses to send output to specific users
    {
            Clients.Client(ClientDirectory[userID]).broadcastMessage(userID, message, target);
    }
    public void CharConnect(string charID, string connID)
        //client uses when first connectiong a character to the server
    {
        ClientDirectory.TryAdd(charID, connID);
        BL.ConnectCharacter(charID);
        BL.SendCommand(charID, "look");

    }
    public void CharDisconnect(string charID)
        //server uses to remove user connection from ClientDirectory when character is logged out and removed from world
    {
        var name = ClientDirectory.FirstOrDefault(x => x.Value == Context.ConnectionId.ToString());
        string s;
        ClientDirectory.TryRemove(name.Key, out s);
    }
}
